#include "reservationsystem.h"

using namespace aviation;
using namespace std;
using namespace boost::archive;

ReservationSystem::ReservationSystem()
{
	// Read in customer database with serialization
	{
		ifstream inStream("customerSerialization.xml");
		if(inStream.good())
		{
			try
			{
				xml_iarchive inArchive(inStream);
				inArchive & BOOST_SERIALIZATION_NVP(customerDB);
			}
			catch(archive_exception e) { cerr << "Fatal Error reading customerSerialization.xml: '" << e.what() << "'.\n"; }
		}
	}

	// Read in flight database with serialization
	{
		ifstream inStream("flightSerialization.xml");
		if(inStream.good())
		{
			try
			{
				xml_iarchive inArchive(inStream);
				inArchive & BOOST_SERIALIZATION_NVP(flightDB);
			}
			catch(archive_exception e) { cerr << "Fatal Error reading flightSerialization.xml: '" << e.what() << "'.\n"; }
		}
	}

	// Read in reservation database with serialization
	{
		ifstream inStream("reservationSerialization.xml");
		if(inStream.good())
		{
			try
			{
				xml_iarchive inArchive(inStream);
				inArchive & BOOST_SERIALIZATION_NVP(reservationDB);
			}
			catch(archive_exception e) { cerr << "Fatal Error reading reservationSerialization.xml: '" << e.what() << "'.\n"; }
		}
	}

	// Read in user database with serialization
	{
		ifstream inStream("userSerialization.xml");
		if(inStream.good())
		{
			try
			{
				xml_iarchive inArchive(inStream);
				inArchive & BOOST_SERIALIZATION_NVP(userDB);
			}
			catch(archive_exception e) { cerr << "Fatal Error reading userSerialization.xml: '" << e.what() << "'.\n"; }
		}
	}
	// Manually read in FCGDATA and add to flightDB if needed
	{
		ifstream inStream("fcgData.txt");
		if(inStream.good())
		{
			unsigned count = 1;
			
			while(!inStream.eof())
			{
				string fcgLine;
				getline(inStream, fcgLine);

				// fcgLine should just be able to immediately instantiate a Flight and add it to the DB.
				// If the data is formatted poorly then we notify the user and skip this line.
				try
				{
					Flight tentativeFlight(fcgLine);
					flightDB.AddData(tentativeFlight);
				}
				catch(exception e) { cerr << "Error reading fcgData.txt at line " << count << ", '" << e.what() << "'.\n"; }
				++count;
			}
		}
	}

}

ReservationSystem::~ReservationSystem()
{
	// Serialize out customer database
	{
		ofstream outStream("customerSerialization.xml");
		if(outStream.good())
		{
			xml_oarchive outArchive(outStream);
			outArchive & BOOST_SERIALIZATION_NVP(customerDB);
		}
	}

	// Serialize out flight database
	{
		ofstream outStream("flightSerialization.xml");
		if(outStream.good())
		{
			xml_oarchive outArchive(outStream);
			outArchive & BOOST_SERIALIZATION_NVP(flightDB);
		}
	}

	// Serialize out reservation database
	{
		ofstream outStream("reservationSerialization.xml");
		if(outStream.good())
		{
			xml_oarchive outArchive(outStream);
			outArchive & BOOST_SERIALIZATION_NVP(reservationDB);
		}
	}

	// Serialize out user database
	{
		ofstream outStream("userSerialization.xml");
		if(outStream.good())
		{
			xml_oarchive outArchive(outStream);
			outArchive & BOOST_SERIALIZATION_NVP(userDB);
		}
	}
}